<?php

namespace Wulian\Controller;

use Common\Controller\AdminbaseController;

class AdminOrderController extends AdminbaseController {

    protected $m_order;

    function _initialize() {
        parent::_initialize();
        $this->m_order = M("WlOrder");

    }

    /**
     * 列表页
     */
    public function index(){

        $s_start_time = I("s_start_time");
        $s_end_time = I("s_end_time");
        $s_number = I("s_number");
        $s_uid = I("s_uid");
        $where = array();

//        if(empty($s_start_time)){
//            $s_start_time = date("Y-m-01");
//            $_GET['s_start_time'] = $s_start_time;
//        }
//        if(empty($s_end_time)){
//            $s_end_time = date("Y-m-d");
//            $_GET['s_end_time'] = $s_end_time;
//        }
//        $str_start_time = strtotime($s_start_time." 00:00:00");
//        $str_end_time = strtotime($s_end_time."23:59:59");
//
//        $where = array(
//            "o.create_time"=>array("between",array($str_start_time,$str_end_time))
//        );
        if(!empty($s_number)){
            $where["o.card_iccid"] = $s_number;
            $_GET['s_number'] = $s_number;
            checkCardNumber($s_number);
        }
        if(!empty($s_uid)){
            $where["c.uid"] = $s_uid;
        }
        $where["o.order_status"] = 1;
        
        $count=$this->m_order->alias("o")->join("cmf_wl_card c ON c.card_number = o.card_number","LEFT")->join("cmf_users u ON u.id = c.uid","LEFT")->where($where)->count();
        $page = $this->page($count, 15);
        $order_list = $this->m_order->alias("o")->field("o.order_number,o.package_type,o.card_number,o.card_iccid,o.package_data,o.order_price,o.order_source,o.create_time,u.user_nickname")->join("cmf_wl_card c ON c.card_number = o.card_number","LEFT")->join("cmf_users u ON u.id = c.uid","LEFT")->where($where)
            ->limit($page->firstRow , $page->listRows)
            ->select();
        foreach($order_list AS &$oitem){
            if($oitem["package_type"] == 1){
                $oitem["package_type"] = "月套餐";
            }elseif($oitem["package_type"] == 2){
                $oitem["package_type"] = "增量包";
            }elseif($oitem["package_type"] == 3){
                $oitem["package_type"] = "年套餐续费";
            }

            $oitem["create_time"] = date("Y-m-d H:i:s",$oitem["create_time"]);

            if($oitem["order_source"] == 1){
                $oitem["order_source"] = "管理员操作";
            }elseif($oitem["order_source"] == 2){
                $oitem["order_source"] = "用户购买";
            }

            $oitem["package_data"] =  ($oitem["package_data"]/$this->c_proportion)."M";
        }

        //设置导出数据
        $export_title = array(
            "user_nickname"=>"卡所属人",
            "order_number"=>"订单号",
            "package_type"=>"类型",
            "card_number"=>"卡号",
            "card_iccid"=>"iccid",
            "package_data"=>"流量值",
            "order_price"=>"价格",
            "order_source"=>"来源",
            "create_time"=>"创建时间",
        );

        $export_list = array(
            "excel_title"=>"订单列表",
            "export_title"=>$export_title,
            "export_data"=>$order_list,
        );
        write_file("order",$export_list);

        $this->assign("page", $page->show('Admin'));
        $this->assign("order_list",$order_list);
        $this->assign("s_number",$s_number);
        $this->assign("s_start_time",$s_start_time);
        $this->assign("s_end_time",$s_end_time);
        $this->assign("s_uid",$s_uid);
        $this->assign("a", "#F{\$dp.\$D('s_end_time')}");
        $this->assign("b", "#F{\$dp.\$D('s_start_time')}");

        $user_list = M("users")->where(array("user_type"=>2))->select();
        $this->assign("user_list", $user_list);
        $this->display();
    }



    // 删除
    public function delete(){
        if(isset($_GET['id'])){
            $id = I("get.id",0,'intval');
            if ($this->m_WlPackage->where(array('package_id'=>$id))->save(array("is_del"=>1)) !==false) {
                $this->success("删除成功！");
            } else {
                $this->error("删除失败！");
            }
        }else{
            $this->error("参数有误！");
        }

    }

}